<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2020/4/1
 * Time: 11:01
 */

namespace tools;


class RoomCache
{
    protected $runtimePath;
    function __construct($runtimePath)
    {
        $this->runtimePath = $runtimePath.'/roomcache/';
    }

    /** 设置缓存
     * @param $data
     * @param $roomid
     */
    function setData($data,$roomid){
        $filename = $this->runtimePath.$roomid.'.txt';
        $dir = dirname($filename);
        if(!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }
        $result = file_put_contents($filename,json_encode($data));
        if ($result) {
            return true;
        }
        return false;
    }

    /** 获取缓存
     * @param $roomid
     * @return bool|mixed
     */
    function getData($roomid){
        $filename = $this->runtimePath.$roomid.'.txt';
        if(!file_exists($filename)){
            return false;
        }
        $f = file_get_contents($filename);
        $old = json_decode($f,true);
        return $old;
    }
}